package bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import luceneutil.V100LunceneWorker;

import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.ScoreDoc;
import org.primefaces.model.chart.PieChartModel;

import applicationutil.ApplicationConstant;
import applicationutil.ApplicationUtil;
import dao.ReportDao;
import dao.V100Dao;
import entities.V100;

@ManagedBean(name = "adminBean")
@ViewScoped
public class AdminBean extends NavigationBean implements Serializable {
	private boolean roleIndexing;
	private List<V100> listV100Src = new ArrayList<V100>();
	private String url;
	private int pv100;
	private String userName;
	private String mess = "";// thong bao tat,mo dcm
	// report
	private PieChartModel chartModel = new PieChartModel();
	private PieChartModel chartModel2 = new PieChartModel();
	private long totalUrl;
	private long totalUrlHasContent;
	private long totalUrlError;
	private long totalUrlIndexed;
	private long totalUrlRemoved;
	private int vn111;
	public AdminBean() {
		// TODO Auto-generated constructor stub
		System.out.println("---vao admin bean");
	}

	private String log = "";

	@PostConstruct
	public void init() {
		V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
		if (ApplicationUtil.getRequestURL().contains("manage_index.xhtml")) {
			int kq = v100Dao.getRoleIndexing();
			if (kq == 1) {
				mess = "Đang bật đánh chỉ mục tự động";
				roleIndexing = false;
			} else {
				mess = "Đang tắt đánh chỉ mục tự động";
				roleIndexing = true;
			}
		} else if (ApplicationUtil.getRequestURL().contains(
				"manage_statistic.xhtml")) {
			loadChart();
		} else if (ApplicationUtil.getRequestURL().contains(
				"manage_search.xhtml"))
			loadListV100Src();

	}
	
	public void loadChart() {
		// setup chart
		ReportDao reportDao = (ReportDao) ApplicationUtil
				.getService("reportDAO");
		totalUrl = reportDao.getTotalUrl();
		totalUrlError = reportDao.getTotalUrlDeleted();
		totalUrlHasContent = reportDao.getTotalUrlHasContent();
		totalUrlIndexed = reportDao.getTotalUrlIndexed();
		totalUrlRemoved = reportDao.getTotalUrlRemoved();
		chartModel.set("Tổng số url chưa lấy nội dung", totalUrl
				- totalUrlHasContent);
		chartModel.set("Tổng số url đã lấy nội dung", totalUrlHasContent);
		chartModel.set("Tổng số url bị lỗi", totalUrlError);
		chartModel.set("Tổng số url bị xóa", totalUrlRemoved);
		chartModel2.set("Tổng số url đã lấy nội dung nhưa chưa đánh chỉ mục", totalUrlHasContent
				- totalUrlIndexed);
		chartModel2.set("Tổng số url đã đánh chỉ mục", totalUrlIndexed);
	}

	public void loadListV100Src() {
		V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
		listV100Src = v100Dao.getListV100Src(0, 10);
		System.out.println("---list src size:" + listV100Src.size());
	}
	public void onClickUpdateV100(int pv100, String url){
		System.out.println("--pv100:"+pv100);
		System.out.println("---url:"+url);
		this.pv100=pv100;
		this.url=url;
	}
	public void reset(){
		this.pv100=0;
		this.url=null;
	}
	public void update() {
		System.out.println("----update role:" + roleIndexing);
		V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
		int role = 0;
		if (roleIndexing == false)
			role = 1;
		v100Dao.updateRoleIndexing(role);
		
		if (role == 1) {
			mess = "Đang bật đánh chỉ mục tự động";
			ApplicationUtil.showMessage("Đã bật đánh chỉ mục tự động!");
		} else {
			mess = "Đang tắt đánh chỉ mục tự động";
			ApplicationUtil.showMessage("Đã tắt đánh chỉ mục tự động!");
		}
	}
	public void deleteV100(){
		try{
			V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
			int kq = v100Dao.remove100Src(pv100);
			if (kq > 0) {
				loadListV100Src();
				ApplicationUtil.showMessage("Đã xóa thành công!");
			} else
				ApplicationUtil.showMessage("Xóa thất bại!");
		}
		catch(Exception ex){
			
		}
	}
	public void insertV100Src() {
		try {
			V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
			int kq = v100Dao.insertV100Src(pv100, url);
			if (kq > 0) {
				loadListV100Src();
				ApplicationUtil.showMessage("Đã cập nhật thành công!");
			} else
				ApplicationUtil.showMessage("Cập nhật thất bại!");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void updateLog() {
		System.out.println("---vao update log");
		log = IndexControl.getLog().toString();
	}

	public void deleteLog() {
		IndexControl.getLog().delete(0, IndexControl.getLog().length());
		log = "";
	}
	
	public void logout() {
		ApplicationUtil.removeSession(ApplicationConstant.ADMIN_NAME);
		ApplicationUtil.navigateTo("login.xhtml");
	}
	/*
	 * xoa nhung chi muc bi loi va xoa trong csdl
	 */
	public void deleteIndexNotConect() {
		try {
			V100Dao v100Dao = (V100Dao) ApplicationUtil.getService("v100DAO");
			List<V100> list = v100Dao.getListV100Error(vn111);
			System.out.println("----so luong url bi loi, vn111:" + list.size()+","+vn111);
			V100LunceneWorker lunceneWorker = new V100LunceneWorker(ApplicationConstant.INDEX_PATH);
			lunceneWorker.createIndex();
			lunceneWorker.createIndexWriter();
			if (list != null && list.size() > 0) {
				for (V100 v100 : list)
					lunceneWorker.delete(v100);
				v100Dao.remove100(vn111);	
				ApplicationUtil.showMessage("Đã xóa thành công!");
			} else
				ApplicationUtil
						.showMessage("Không có url nào không kết nối được!");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public boolean isRoleIndexing() {
		return roleIndexing;
	}

	public void setRoleIndexing(boolean roleIndexing) {
		this.roleIndexing = roleIndexing;
	}

	public List<V100> getListV100Src() {
		return listV100Src;
	}

	public void setListV100Src(List<V100> listV100Src) {
		this.listV100Src = listV100Src;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUserName() {
		if (ApplicationUtil.getSession(ApplicationConstant.ADMIN_NAME) != null)
			return ApplicationUtil.getSession(ApplicationConstant.ADMIN_NAME)
					.toString();
		else
			return "";
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getLog() {
		return log;
	}

	public void setLog(String log) {
		this.log = log;
	}

	public String getMess() {
		return mess;
	}

	public void setMess(String mess) {
		this.mess = mess;
	}

	public PieChartModel getChartModel() {
		return chartModel;
	}

	public void setChartModel(PieChartModel chartModel) {
		this.chartModel = chartModel;
	}

	public long getTotalUrl() {
		return totalUrl;
	}

	public void setTotalUrl(long totalUrl) {
		this.totalUrl = totalUrl;
	}

	public long getTotalUrlHasContent() {
		return totalUrlHasContent;
	}

	public void setTotalUrlHasContent(long totalUrlHasContent) {
		this.totalUrlHasContent = totalUrlHasContent;
	}

	

	public long getTotalUrlError() {
		return totalUrlError;
	}

	public void setTotalUrlError(long totalUrlError) {
		this.totalUrlError = totalUrlError;
	}

	public long getTotalUrlIndexed() {
		return totalUrlIndexed;
	}

	public void setTotalUrlIndexed(long totalUrlIndexed) {
		this.totalUrlIndexed = totalUrlIndexed;
	}

	public PieChartModel getChartModel2() {
		return chartModel2;
	}

	public void setChartModel2(PieChartModel chartModel2) {
		this.chartModel2 = chartModel2;
	}

	public long getTotalUrlRemoved() {
		return totalUrlRemoved;
	}

	public void setTotalUrlRemoved(long totalUrlRemoved) {
		this.totalUrlRemoved = totalUrlRemoved;
	}

	public int getVn111() {
		return vn111;
	}

	public void setVn111(int vn111) {
		this.vn111 = vn111;
	}
	
}
